//
//  PhotoViewController.m
//  Streaming
//
//  Created by 官琛 on 15-6-24.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "PhotoViewController.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController

- (void)dealloc
{
    [self.collectionView release];
    [self.array release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self afnGetRequest];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor blackColor];
    [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"Photo"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    flowLayout.itemSize = CGSizeMake(100, 100);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    [_collectionView release];
    [flowLayout release];
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoDetailsController *view = [[PhotoDetailsController alloc]init];
    view.array = self.array;
    view.indexImage = [self.array objectAtIndex:indexPath.row];
    view.row = indexPath.row;
//    NSLog(@"%ld",indexPath.row);
    [self.navigationController pushViewController:view animated:YES];
    [view release];

}

//ITem数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Photo" forIndexPath:indexPath];
    
    Photo *pho = [self.array objectAtIndex:indexPath.row];
    cell.photo = pho;
    
    return cell;


}

-(void)afnGetRequest
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/Movie/ImageAll.api?movieId=%ld",self.movieId];
 
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableArray *array = [NSMutableArray array];
        self.array = [NSMutableArray array];
        array = [responseObject objectForKey:@"images"];
        for (NSMutableDictionary *dic in array) {
           
            Photo *pho = [[Photo alloc]init];
            pho.photoId = [[dic objectForKey:@"id"]integerValue];
            pho.type = [[dic objectForKey:@"type"]integerValue];
            pho.image = [dic objectForKey:@"image"];
            pho.tag = pho.photoId;
            [self.array addObject:pho];
            [pho release];
            
        }
        
        [self.collectionView reloadData];
//        [self.HUD hide:YES];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
//        [self.HUD hide:YES];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
